package com.wintop.youyuan.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.wintop.youyuan.model.BaseData;
import com.wintop.youyuan.model.DetailData;
import com.wintop.youyuan.model.PersonalsCondition;
import com.wintop.youyuan.model.User;
import com.wintop.youyuan.services.UserServices;

@Scope("prototype")
@Controller
public class ManageAction extends ActionSupport implements RequestAware,
		SessionAware, ApplicationAware, ServletResponseAware {
	private static final long serialVersionUID = 1L;
	private final int pageSize = 12;// 页数大小
	private Map<String, Object> request;
	private Map<String, Object> session;
	private Map<String, Object> application;
	private HttpServletResponse response;
	private User user;
	private UserServices userServices;
	private BaseData baseData;
	private DetailData detailData;
	private PersonalsCondition personalsCondition;
	private HashMap<String, Object> otherData;// 临时数据储存

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public String redirectManage() {// 管理首页
		/*
		 * User userInfo = new User(31, "xu@qq.com",
		 * "e10adc3949ba59abbe56e057f20f883e", "许峰", 0, 1, null);
		 * session.put("userInfo", userInfo);
		 */
		if (session.get("userInfo") == null) {// 判断用户是否登录
			return "error";
		}
		return "redirectManage";
	}

	public String redirectUserList() {// 用户列表
		if (otherData == null) {
			otherData = new HashMap<String, Object>();
			otherData.put("pageNum", "1");
		}
		List<User> userList = userServices.getUsers();
		int userListSize = userList.size();
		otherData.put("pages", (int) Math.ceil(userListSize / pageSize) + 1);
		List<User> userListNew = new ArrayList<User>();
		int pageNum = Integer.parseInt((String) otherData.get("pageNum")) - 1;
		int maxSize = ((pageNum + 1) * pageSize) < userList.size() ? ((pageNum + 1) * pageSize)
				: userList.size();
		for (int i = pageNum * pageSize; i < maxSize; i++) {
			userListNew.add(userList.get(i));
		}
		otherData.put("userList", userListNew);
		otherData.put("userListSize", userListSize);

		return "redirectUserList";
	}

	public String redirectAfterUser() {// 跳转添加或修改用户数据页面
		if (user != null && user.getId() != 0) {
			user = userServices.getUser(user.getId());
		}
		return "redirectAfterUser";
	}

	public String AfterUser() {// 添加或修改用户数据
		if (user.getId() == 0) {
			userServices.register(user, 21);
		} else {
			userServices.updateUser(user);
		}
		return redirectUserList();
	}

	public HashMap<String, Object> getOtherData() {
		return otherData;
	}

	public void setOtherData(HashMap<String, Object> otherData) {
		this.otherData = otherData;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Resource(name = "userServices")
	public void setUserServices(UserServices userServices) {
		this.userServices = userServices;
	}

	public BaseData getBaseData() {
		return baseData;
	}

	public void setBaseData(BaseData baseData) {
		this.baseData = baseData;
	}

	public DetailData getDetailData() {
		return detailData;
	}

	public void setDetailData(DetailData detailData) {
		this.detailData = detailData;
	}

	public PersonalsCondition getPersonalsCondition() {
		return personalsCondition;
	}

	public void setPersonalsCondition(PersonalsCondition personalsCondition) {
		this.personalsCondition = personalsCondition;
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public Map<String, Object> getApplication() {
		return application;
	}

	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

}
